1195B - Sport Mafia - CodeForces Solution


binary search brute force math *1000

Please click on ads to support us..

Python Code:


n, k = input().split(" ")
n = int(n)
k = int(k)

def works(value):
    total_candies_provisioned = (value * (value + 1)) // 2
        return total_candies_provisioned - (n - value) - k

def binary_search():
    if n == 1:
        return 0

    L = 0
    R = 10 ** 9

    while L <= R:
        mid = (L + R) // 2
        result = works(mid)

                                
        if result == 0:
            return n - mid
        elif result < 0:
            L = mid + 1
        else:
            R = mid - 1

print(binary_search())

C++ Code:

#include<bits/stdc++.h>
#define ll long long int
#define whole(v) v.begin(),v.end()
#define rwhole(v) v.rbegin(),v.rend()
#define endl '\n'
#define pb push_back
#define Speedforce ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

ll egcd(ll a, ll b)
{
    if(a%b == 0) return b;
    else return egcd(b, a%b);
}

int main()
{
    Speedforce;
    ll t, n, d, i, j, k, x, y, tmp, tmp1;
    cin >> x >> y;
    
    ll high = x, low = 1, mid = (high+low)/2;
    d = (mid*(mid+1)/2);
    ll ate = x-mid;
    ll left = d-ate;
    while(1)
    {
        if(left == y) break;
        else if(left > y)
        {
            high = mid;
            mid = (high+low)/2;
            d = (mid*(mid+1)/2);
            ate = x - mid;
            left = d-ate;
        }
        else
        {
            low = mid;
            mid = (high+low)/2;
            d = (mid*(mid+1)/2);
            ate = x - mid;
            left = d-ate;
        }

    }
    cout << ate << endl;



}


Comments

Submit
0 Comments
More Questions

1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas